package com.apobates.jforum2.troll.threads.mapper;

import com.apobates.jforum2.threads.stub.vo.ThreadsVo;
import com.apobates.jforum2.troll.threads.entity.Topic;
import com.apobates.jforum2.troll.utils.core.DateTimeUtils;
import com.apobates.jforum2.troll.utils.core.api.OptionalEntityTransformMapper;
import com.apobates.jforum2.troll.utils.lang.TriFunction;
import java.util.Optional;
import java.util.function.Function;

/**
 *
 * @author xiaofanku
 */
public class ThreadsVoMapperEntity implements OptionalEntityTransformMapper<Topic, ThreadsVo>{
    public static TriFunction<Long,Long,Integer,String> produceLink = (Long topicId, Long boardId, Integer boardgroupId)->{
        return String.format("/topic/%d-%d-%d.xhtml", topicId, boardId, boardgroupId);
    };
    @Override
    public Optional<ThreadsVo> transform(Optional<Topic> entity) {
        return entity.map(ins->{
            return mapper().apply(ins);
        });
    }
    /**
     * 是否需要增加话题(像册)封面图片
     * @since 20201125
     * @return
     */
    public static Function<Topic, ThreadsVo> mapper(){
        return topic -> {
            ThreadsVo tv = new ThreadsVo();
            tv.setId(topic.getId());
            //主题
            tv.setTitle(topic.getTitle());
            //发布日期
            tv.setEntryDateTime(topic.getEntryDateTime());
            //作者:会员的ID
            tv.setMember(topic.getMemberId());
            tv.setNickname(topic.getMemberNickname());
            tv.setIpAddr(topic.getIpAddr());
            //卷的ID
            tv.setVolumes(topic.getVolumesId());
            //版块的ID
            tv.setBoard(topic.getBoardId());
            //置顶(时效),true(1)置顶,false(0)不置顶
            tv.setTops(topic.isTops());
            //加精,true(1)精华,false(0)非精华
            tv.setGoods(topic.isGoods());
            //火贴,true(1)火,false(0)没火
            tv.setHots(topic.isHots());
            //是否有像册@20190412
            tv.setAlbum(topic.getAlbumId());
            //状态
            tv.setStatus(topic.getStatus().getSymbol());
            tv.setStatusTitle(topic.getStatus().getTitle());
            //显示顺序:普通话题是1,置顶的依次++
            tv.setRanking(topic.getRanking());
            //排序的日期
            //没有回复时为entryDateTime
            //有回复时为最近的回复日期
            tv.setRankingDateTime(topic.getRankingDateTime());
            //话题类型的参数名,年代
            tv.setCategoryName(topic.getTopicCategoryName());
            //话题类型的参数值,year
            tv.setCategoryValue(topic.getTopicCategoryValue());
            //简述|举报和投拆不要设置此值
            tv.setSummary(topic.getSummary());
            //是否匿名,true是,false不是
            tv.setAnony(topic.isAnony());
            //访问连接
            tv.setLink(produceLink.apply(topic.getId(), topic.getBoardId(), topic.getVolumesId()));
            //是今日的话题吗
            tv.setToday(DateTimeUtils.isSameDay(topic.getEntryDateTime()));
            return tv;
        };
    }
}